package com.ruoyi.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRegion;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.system.mapper.SysRegionMapper;
import com.ruoyi.system.service.ISysRegionService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 区域字典Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements ISysRegionService {


    @Override
    public SysRegion queryGetById(String id) {
        SysRegion sysRegion = this.getById(id);
        return sysRegion;
    }

    @Override
    public List<SysRegion> queryChildrenByParentId(String parentId) {
        List<SysRegion> regionList = this.list(new QueryWrapper<SysRegion>().eq("parent_id", parentId));
        return regionList;
    }

    @Override
    public List<Tree<String>> getNodeTree() {
        List<SysRegion> nodeList = this.list();

        List<TreeNode> transNodes = nodeList.stream().map(m -> {
            TreeNode node = new TreeNode();
            node.setId(m.getId());
            node.setParentId(m.getParentId());
            node.setName(m.getRegionName());

            // 设置扩展属性到 extra（Map 类型）
            Map<String, Object> extra = new HashMap<>();
            extra.put("regionCode", m.getRegionCode());
            extra.put("level", m.getLevel());
            // 其他字段...
            node.setExtra(extra);

            return node;
        }).collect(Collectors.toList());

// 配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setIdKey("value");

// ✅ 使用 Supplier 作为 factory 创建 RegionTree 实例
        List<Tree<String>> treeNodes = TreeUtil.<TreeNode, String>build(transNodes, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId((String) treeNode.getId());
                    tree.setParentId((String) treeNode.getParentId());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    Map extra = treeNode.getExtra();
                    tree.putExtra("regionCode", extra.get("regionCode"));
                    tree.putExtra("level", extra.get("level"));
                });
        return treeNodes;
    }

    /**
     * 新增区域字典
     *
     * @param sysRegion 区域字典
     * @return 结果
     */
    @Override
    public boolean addSysRegion(SysRegion sysRegion) {
        boolean save = this.save(sysRegion);
        return save;
    }

    /**
     * 修改区域字典
     *
     * @param sysRegion 区域字典
     * @return 结果
     */
    @Override
    public boolean updateSysRegion(SysRegion sysRegion) {
        return this.updateById(sysRegion);
    }

    /**
     * 批量删除区域字典
     *
     * @param ids 需要删除的区域字典主键
     * @return 结果
     */
    @Override
    public boolean deleteSysRegionByIds(String[] ids) {
        return this.removeByIds(ListUtil.toList(ids));
    }

}

